package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.qurey.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHepler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service("destinationService")
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository destinationRepository;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        Destination destination = destinationRepository.findById(id).get();
        return destination;
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public List<Destination> queryDestByRegionId(String rid) {
        //查询区域下的目的地id集合
        Region region = regionService.get(rid);
        List<String> refIds = region.getRefIds();
        List<Destination> byIdIn = destinationRepository.findByIdIn(refIds);
        //以目的地id集合,去查询
        return destinationRepository.findByIdIn(refIds);
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {
        Query query = new Query();
        //高级查询
        if (StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        //通过父级id逐渐向下查询
        if (StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }

        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHepler.query(template, pageRequest, query, Destination.class, qo);
    }

    @Override
    public List<Destination> queryToasts(String destId) {
        List<Destination> list = new ArrayList<>();

        if (!StringUtils.hasLength(destId)){
            return list;
        }

        Destination destination = this.get(destId);
        list.add(destination);

        while (destination.getParentId()!=null){
            Destination destination1 = this.get(destination.getParentId());
            list.add(destination1);
            destination = destination1;
        }

        //this.createToasts(list,destId);

        Collections.reverse(list);
        return list;
    }

    @Override
    public List<Destination> queryDestByRegionIdForApi(String regionId) {
        List<Destination> list = new ArrayList<>();
        if ("-1".equals(regionId)){
            //国内
            list = destinationRepository.findByParentName("中国");
        }else {
            //国外
            list = this.queryDestByRegionId(regionId);
        }
        //通过目的地查询子目的地
        for (Destination destination : list) {
            List<Destination> byParentId = destinationRepository.findByParentId(destination.getId());
            destination.setChildren(byParentId);
        }

        return list;

    }

    @Override
    public Destination findByName(String keyword) {
        Destination dest=destinationRepository.findByName(keyword);
        return dest;
    }


    private void createToasts(List<Destination> list,String destId){
        if (!StringUtils.hasLength(destId)){
            return;
        }

        Destination destination = this.get(destId);
        list.add(destination);

        if (StringUtils.hasLength(destination.getParentId())){
            this.createToasts(list, destination.getParentId());
        }

    }


}
